package com.byb.server.web.rest;

import com.byb.server.service.dto.Wc.InsuranceForWcDTO;
import com.codahale.metrics.annotation.Timed;
import com.byb.server.service.WcIsurerService;
import com.byb.server.web.rest.util.HeaderUtil;
import com.byb.server.web.rest.util.PaginationUtil;
import com.byb.server.service.dto.WcIsurerDTO;
import io.swagger.annotations.ApiParam;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * REST controller for managing WcIsurer.
 */
@RestController
@RequestMapping("/api")
public class WcIsurerResource {

    private final Logger log = LoggerFactory.getLogger(WcIsurerResource.class);

    private static final String ENTITY_NAME = "wcIsurer";

    private final WcIsurerService wcIsurerService;

    public WcIsurerResource(WcIsurerService wcIsurerService) {
        this.wcIsurerService = wcIsurerService;
    }

    /**
     * POST  /wc-isurers : Create a new wcIsurer.
     *
     * @param wcIsurerDTO the wcIsurerDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new wcIsurerDTO, or with status 400 (Bad Request) if the wcIsurer has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/wc-isurers")
    @Timed
    public ResponseEntity<WcIsurerDTO> createWcIsurer(@RequestBody WcIsurerDTO wcIsurerDTO) throws URISyntaxException {
        log.debug("REST request to save WcIsurer : {}", wcIsurerDTO);
        if (wcIsurerDTO.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new wcIsurer cannot already have an ID")).body(null);
        }
        WcIsurerDTO result = wcIsurerService.save(wcIsurerDTO);
        return ResponseEntity.created(new URI("/api/wc-isurers/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /wc-isurers : Updates an existing wcIsurer.
     *
     * @param wcIsurerDTO the wcIsurerDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated wcIsurerDTO,
     * or with status 400 (Bad Request) if the wcIsurerDTO is not valid,
     * or with status 500 (Internal Server Error) if the wcIsurerDTO couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/wc-isurers")
    @Timed
    public ResponseEntity<WcIsurerDTO> updateWcIsurer(@RequestBody WcIsurerDTO wcIsurerDTO) throws URISyntaxException {
        log.debug("REST request to update WcIsurer : {}", wcIsurerDTO);
        if (wcIsurerDTO.getId() == null) {
            return createWcIsurer(wcIsurerDTO);
        }
        WcIsurerDTO result = wcIsurerService.save(wcIsurerDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, wcIsurerDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /wc-isurers : get all the wcIsurers.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of wcIsurers in body
     */
    @GetMapping("/wc-isurers")
    @Timed
    public ResponseEntity<List<WcIsurerDTO>> getAllWcIsurers(@ApiParam Pageable pageable) {
        log.debug("REST request to get a page of WcIsurers");
        Page<WcIsurerDTO> page = wcIsurerService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/wc-isurers");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /wc-isurers/:id : get the "id" wcIsurer.
     *
     * @param id the id of the wcIsurerDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the wcIsurerDTO, or with status 404 (Not Found)
     */
    @GetMapping("/wc-isurers/{id}")
    @Timed
    public ResponseEntity<WcIsurerDTO> getWcIsurer(@PathVariable Long id) {
        log.debug("REST request to get WcIsurer : {}", id);
        WcIsurerDTO wcIsurerDTO = wcIsurerService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(wcIsurerDTO));
    }

    /**
     * DELETE  /wc-isurers/:id : delete the "id" wcIsurer.
     *
     * @param id the id of the wcIsurerDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/wc-isurers/{id}")
    @Timed
    public ResponseEntity<Void> deleteWcIsurer(@PathVariable Long id) {
        log.debug("REST request to delete WcIsurer : {}", id);
        wcIsurerService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

    /**
     * SEARCH  /_search/wc-isurers?query=:query : search for the wcIsurer corresponding
     * to the query.
     *
     * @param query the query of the wcIsurer search
     * @param pageable the pagination information
     * @return the result of the search
     */
    @GetMapping("/_search/wc-isurers")
    @Timed
    public ResponseEntity<List<WcIsurerDTO>> searchWcIsurers(@RequestParam String query, @ApiParam Pageable pageable) {
        log.debug("REST request to search for a page of WcIsurers for query {}", query);
        Page<WcIsurerDTO> page = wcIsurerService.search(query, pageable);
        HttpHeaders headers = PaginationUtil.generateSearchPaginationHttpHeaders(query, page, "/api/_search/wc-isurers");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /wc-isurers : get all the wcIsurers.
     *
     * @return the ResponseEntity with status 200 (OK) and the list of wcIsurers in body
     */
    @GetMapping("/wc-isurer-data")
    @Timed
    public ResponseEntity<InsuranceForWcDTO> getAll() {
        log.debug("REST request to get a page of InsuranceForWcDTO for wc");
        InsuranceForWcDTO insuranceForWcDTO = wcIsurerService.findAllForWc();
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(insuranceForWcDTO));
    }
}
